#!/usr/bin/env python
# coding:utf-8

import pymysql, sys


# sql=sys.argv[1]

def select_mysql(sql):
    conn = pymysql.connect(host='121.201.68.21', port=3307, user='jiang', passwd='jiangwenhui', db='trans_data',
                           charset='UTF8')

    cur = conn.cursor()
    reCount = cur.execute(sql)

    nRet = cur.fetchall()
    cur.close()

    conn.close()
    return nRet


def insert_mysql(sql):
    conn = pymysql.connect(host='121.201.68.21', port=3307, user='jiang', passwd='jiangwenhui', db='daxiangzhanshi',

                           charset='UTF8')

    cur = conn.cursor()

    cur.execute(sql)
    nRet = cur.fetchall()
    conn.commit()

    cur.close()

    conn.close()
    return nRet

if __name__ == "__main__":
    insert_mysql("delete from app01_city_trading_volume")
    select_ls_category = "select city,sum(total)  from ls_cities as l,sub_city as s where l.id=s.city_id and time_>=curdate()  group by city "
    data = select_mysql(select_ls_category)
    for i in data:
#	print i
        insert_ls_category = '''insert into app01_city_trading_volume (city_name,city_rmb,updatetime) values ('%s','%s',curdate()) '''%(i[0],i[1])
        insert_ls_category=insert_ls_category.replace("市",'')
        insert_mysql(insert_ls_category)
            #if not insert_mysql("select * from app01_real where updatetime='%s'"%i[3]):
            #insert_mysql(insert_ls_category)
	#print(insert_ls_category)
